package util

import (
	"bufio"
	"fmt"
	"gitee.com/kristas/booting-go/framework/common/util/convert"
	"os"
	"reflect"
	"strings"
)

func Scan(placeHolder string, ptr interface{}) error {
	reader := bufio.NewReader(os.Stdin)
	fmt.Println(placeHolder)
	text, err := reader.ReadString('\n')
	if err != nil {
		return err
	}
	text = strings.ReplaceAll(text, "\n", "")
	text = strings.ReplaceAll(text, "\r", "")
	r, err := convert.Convert(text, reflect.TypeOf(ptr).Elem())
	if err != nil {
		return err
	}
	reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(r))
	return nil
}
